package com.ksr.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.ksr.logistics.mapper.PermissionMapper;
import com.ksr.logistics.pojo.Permission;
import com.ksr.logistics.pojo.PermissionExample;
import com.ksr.logistics.pojo.Result;
import com.ksr.logistics.service.PermissionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> selPermissions() {
        PermissionExample example=new PermissionExample();
        return permissionMapper.selectByExample(example);
    }

    @Override
    public List<Permission> selPagePermissions(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer pageSize, String keyWord) {
        PermissionExample example=new PermissionExample();
        if (StringUtils.isNotBlank(keyWord)){
            PermissionExample.Criteria criteria=example.createCriteria();
            criteria.andNameLike("%"+keyWord+"%");
        }
        PageHelper.startPage(page,pageSize);
        List<Permission> permissions = permissionMapper.selectByExample(example);
        return permissions;
    }

    @Override
    public Result deleteById(Integer id) {
        Result result=new Result();
        List<Permission> permissions=selChilds(id);
        if (permissions.size()>0){
            result.setCode(0);
            result.setMsg("该权限有正在使用的子权限<br>请删除子权限后重试!");
            return result;
        }else {
            int res= permissionMapper.deleteByPrimaryKey((long)id);
            if (1==res){
                result.setCode(1);
                result.setMsg("删除成功!");
            }else{
                result.setCode(0);
                result.setMsg("删除失败,请重试");
            }
            return result;
        }
    }

    @Override
    public Result deleteByIds(List<Integer> ids) {
        Result result=new Result();
        int res= 0;
        int count=ids.size();
        for (int i = 0; i < count; i++) {
            if (ids.size()==0){
                break;
            }
            List<Integer> newIds=new ArrayList<>();
            for (Integer id:ids) {
                if (1==deleteById(id).getCode()){
                    res++;
                   newIds.add(id);
                }
            }
            if (newIds.size()>0){
                ids.removeAll(newIds);
            }
        }
        if (1<=res){
            result.setCode(1);
            if (count==res){
                result.setMsg("成功删除所有选中权限!");
            }else{
                result.setMsg("成功删除权限"+res+"个<br>部分权限有正在使用的子权限,请删除子权限后重试!");
            }
        }else{
            result.setCode(0);
            result.setMsg("被选择权限有正在使用的子权限<br>请删除子权限后重试");
        }
        return result;
    }

    @Override
    public Permission selById(Integer id) {
        return permissionMapper.selectByPrimaryKey((long)id);
    }

    @Override
    public int insertPermission(Permission permission) {
        return permissionMapper.insert(permission);
    }

    @Override
    public int updatePermission(Permission permission) {
        return permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public List<Permission> selByPermissionName(String name) {
        PermissionExample example=new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        return permissionMapper.selectByExample(example);
    }

    @Override
    public List<Permission> selChilds(Integer id) {
        PermissionExample example=new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo((long)id);
        return permissionMapper.selectByExample(example);
    }

    @Override
    public List<Permission> selIn(String ids) {
        PermissionExample example=new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        List<Long> values=new ArrayList<>();
        if (StringUtils.isNotBlank(ids)){
            String[] idArr = ids.split(",");
            for (int i = 0; i < idArr.length; i++) {
                if (StringUtils.isNumeric(idArr[i])){
                    values.add(Long.parseLong(idArr[i]));
                }
            }
        }else {
            long none=-100;
            values.add(none);
        }
        criteria.andPermissionIdIn(values);
        return permissionMapper.selectByExample(example);
    }


    @Override
    public List<String> selByPermissionIds(List<Long> ids) {

        return permissionMapper.selByPermissionIds(ids);
    }

    @Override
    public List<Permission> selMenus(Long parentId, String permissionIds) {
//        List<Permission> relist=new ArrayList<>();
//        List<Permission> permissions = permissionMapper.selMenus();
//        String[] ids = permissionIds.split(",");
//        List<String> list= Arrays.asList(ids);
//        for (Permission permissopn:permissions) {
//            if (!list.contains(permissopn.getPermissionId()+"")){
//                relist.add(permissopn);
//            }
//        }
//        permissions.removeAll(relist);
//        relist.clear();
//        for (Permission permisson:permissions) {
//            if (permisson.getParentId()==0){
//                for (Permission p2:permissions) {
//                    if (p2.getParentId().equals(permisson.getPermissionId())){
//                        permisson.getSubMenus().add(p2);
//                        relist.add(p2);
//                    }
//                }
//            }
//        }
//        permissions.removeAll(relist);

        List<Permission> permissions = permissionMapper.selMenus(parentId, permissionIds);
        return permissions;
    }

    @Override
    public List<Permission> selMenus(Long parentId, Long roleId) {
        return permissionMapper.getMenus(parentId,roleId);
    }
}
